package com.gxar.quick.ar.server.base.controller;

import com.gxar.common.entity.PageResult;
import com.gxar.common.entity.Result;
import com.gxar.common.entity.SystemCodeEnum;
import com.gxar.common.entity.login.LoginUserThread;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.quick.ar.server.base.common.properties.UserProperties;
import com.gxar.quick.ar.server.base.model.works.vo.request.WorksTemplateCreateRequest;
import com.gxar.quick.ar.server.base.model.works.vo.request.WorksTemplateUpdateRequest;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksTemplateCreateResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksTemplateInfoResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksTemplateListResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksTemplateUpdateResponse;
import com.gxar.quick.ar.server.db.service.WorksTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/1/26 14:02
 */
@Api(tags = "管理端作品模板接口")
@RestController
@RequestMapping("/admin/api/works/template")
@RequiredArgsConstructor
@Slf4j
public class AdminWorksTemplateController {

    private final UserProperties userProperties;
    private final WorksTemplateService worksTemplateService;

    @ApiOperation("作品模板列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_num", value = "页码", dataType = "number"),
            @ApiImplicitParam(name = "page_size", value = "页记录数", dataType = "number"),
            @ApiImplicitParam(name = "product_category", value = "产品类别，0-QuickAR,1-货+AR", dataType = "string"),
    })
    @GetMapping("")
    public Result<PageResult<WorksTemplateListResponse>> pageList(@RequestParam(name = "page_num", defaultValue = "1") int pageNum,
                                                                  @RequestParam(name = "page_size", defaultValue = "10") int pageSize,
                                                                  @RequestParam(name = "product_category", required = false) String productCategory) {
        UserProfile userProfile = LoginUserThread.get();
        return worksTemplateService.pageList(userProfile, pageNum, pageSize, productCategory);
    }

    @ApiOperation("查看模板详情")
    @ApiImplicitParam(name = "work_template_id", value = "作品模板id", dataType = "long", required = true, paramType = "path")
    @GetMapping("/{work_template_id}")
    public Result<WorksTemplateInfoResponse> getTemplateDetail(@PathVariable(name = "work_template_id") String id) {
        UserProfile userProfile = LoginUserThread.get();
        if (StringUtils.isBlank(id) || "null".equals(id)) {
            return Result.error(SystemCodeEnum.PARAM_ERROR);
        }
        return worksTemplateService.getTemplateDetail(userProfile, Long.parseLong(id));
    }

    @ApiOperation("创建模板")
    @PostMapping("")
    public Result<WorksTemplateCreateResponse> createTemplate(@RequestBody WorksTemplateCreateRequest request) {
        UserProfile userProfile = LoginUserThread.get();
        WorksTemplateCreateResponse response = worksTemplateService.createTemplate(userProfile, request);
        return Result.success(response);
    }

    @ApiOperation("更新模板")
    @ApiImplicitParam(name = "work_template_id", value = "作品模板id", dataType = "long", required = true, paramType = "path")
    @PutMapping("/{work_template_id}")
    public Result<WorksTemplateUpdateResponse> updateTemplate(@PathVariable(name = "work_template_id") String id, @RequestBody WorksTemplateUpdateRequest request) {
        if (StringUtils.isBlank(id) || "null".equals(id)) {
            return Result.error(SystemCodeEnum.PARAM_ERROR);
        }
        UserProfile userProfile = LoginUserThread.get();
        WorksTemplateUpdateResponse response = worksTemplateService.updateTemplate(userProfile, id, request);
        return Result.success(response);
    }

    @ApiOperation("删除模板")
    @ApiImplicitParam(name = "work_template_id", value = "作品模板id", dataType = "long", required = true, paramType = "path")
    @DeleteMapping("/{work_template_id}")
    public Result<Boolean> deleteTemplate(@PathVariable(name = "work_template_id") String id) {
        if (StringUtils.isBlank(id) || "null".equals(id)) {
            return Result.error(SystemCodeEnum.PARAM_ERROR);
        }
        UserProfile userProfile = LoginUserThread.get();
        worksTemplateService.deleteTemplate(userProfile, id);
        return Result.success("删除成功", true);
    }
}
